<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Validations::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Validations::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/validations_rb.html">
                vendor/rails/activerecord/lib/active_record/validations.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Active Record classes can implement validations in several ways. The
highest level, easiest to read, and recommended approach is to use the
declarative <tt>validates_..._of</tt> class methods (and <tt><a
href="ClassMethods.html#M001382">validates_associated</a></tt>) documented
below. These are sufficient for most model validations.
</p>
<p>
Slightly lower level is <tt><a
href="ClassMethods.html#M001372">validates_each</a></tt>. It provides some
of the same options as the purely declarative validation methods, but like
all the lower-level approaches it requires manually adding to the errors
collection when the record is invalid.
</p>
<p>
At a yet lower level, a model can use the class methods <tt>validate</tt>,
<tt>validate_on_create</tt> and <tt>validate_on_update</tt> to add
validation methods or blocks. These are <a
href="../../ActiveSupport/Callbacks.html">ActiveSupport::Callbacks</a> and
follow the same rules of inheritance and chaining.
</p>
<p>
The lowest level style is to define the instance methods <tt>validate</tt>,
<tt>validate_on_create</tt> and <tt>validate_on_update</tt> as documented
in <a href="../Validations.html">ActiveRecord::Validations</a>.
</p>
<h2><tt>validate</tt>, <tt>validate_on_create</tt> and <tt>validate_on_update</tt> Class Methods</h2>
<p>
Calls to these methods add a validation method or block to the class.
Again, this approach is recommended only when the higher-level methods
documented below (<tt>validates_..._of</tt> and <tt><a
href="ClassMethods.html#M001382">validates_associated</a></tt>) are
insufficient to handle the required validation.
</p>
<p>
This can be done with a symbol pointing to a method:
</p>
<pre>
  class Comment &lt; ActiveRecord::Base
    validate :must_be_friends

    def must_be_friends
      errors.add_to_base(&quot;Must be friends to leave a comment&quot;) unless commenter.friend_of?(commentee)
    end
  end
</pre>
<p>
Or with a block which is passed the current record to be validated:
</p>
<pre>
  class Comment &lt; ActiveRecord::Base
    validate do |comment|
      comment.must_be_friends
    end

    def must_be_friends
      errors.add_to_base(&quot;Must be friends to leave a comment&quot;) unless commenter.friend_of?(commentee)
    end
  end
</pre>
<p>
This usage applies to <tt>validate_on_create</tt> and
<tt>validate_on_update</tt> as well.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001384">create!</a>&nbsp;&nbsp;
      <a href="#M001374">validates_acceptance_of</a>&nbsp;&nbsp;
      <a href="#M001382">validates_associated</a>&nbsp;&nbsp;
      <a href="#M001373">validates_confirmation_of</a>&nbsp;&nbsp;
      <a href="#M001372">validates_each</a>&nbsp;&nbsp;
      <a href="#M001381">validates_exclusion_of</a>&nbsp;&nbsp;
      <a href="#M001379">validates_format_of</a>&nbsp;&nbsp;
      <a href="#M001380">validates_inclusion_of</a>&nbsp;&nbsp;
      <a href="#M001376">validates_length_of</a>&nbsp;&nbsp;
      <a href="#M001383">validates_numericality_of</a>&nbsp;&nbsp;
      <a href="#M001375">validates_presence_of</a>&nbsp;&nbsp;
      <a href="#M001377">validates_size_of</a>&nbsp;&nbsp;
      <a href="#M001378">validates_uniqueness_of</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">


    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">DEFAULT_VALIDATION_OPTIONS</td>
          <td>=</td>
          <td class="context-item-value">{         :on =&gt; :save,         :allow_nil =&gt; false,         :allow_blank =&gt; false,         :message =&gt; nil</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">ALL_RANGE_OPTIONS</td>
          <td>=</td>
          <td class="context-item-value">[ :is, :within, :in, :minimum, :maximum ].freeze</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">ALL_NUMERICALITY_CHECKS</td>
          <td>=</td>
          <td class="context-item-value">{ :greater_than =&gt; '&gt;', :greater_than_or_equal_to =&gt; '&gt;=',                                   :equal_to =&gt; '==', :less_than =&gt; '&lt;', :less_than_or_equal_to =&gt; '&lt;=',                                   :odd =&gt; 'odd?', :even =&gt; 'even?' }.freeze</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001384" class="method-detail">
        <a name="M001384"></a>

        <div class="method-heading">
          <a href="#M001384" class="method-signature">
          <span class="method-name">create!</span><span class="method-args">(attributes = nil) {|object| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates an object just like <a href="../Base.html#M001680">Base.create</a>
but calls save! instead of save so an exception is raised if the record is
invalid.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001384-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001384-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 984</span>
984:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create!</span>(<span class="ruby-identifier">attributes</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
985:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)
986:           <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> <span class="ruby-identifier">create!</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) }
987:         <span class="ruby-keyword kw">else</span>
988:           <span class="ruby-identifier">object</span> = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">attributes</span>)
989:           <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">object</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
990:           <span class="ruby-identifier">object</span>.<span class="ruby-identifier">save!</span>
991:           <span class="ruby-identifier">object</span>
992:         <span class="ruby-keyword kw">end</span>
993:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001374" class="method-detail">
        <a name="M001374"></a>

        <div class="method-heading">
          <a href="#M001374" class="method-signature">
          <span class="method-name">validates_acceptance_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encapsulates the pattern of wanting to validate the acceptance of a terms
of service check box (or similar agreement). Example:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_acceptance_of :terms_of_service
    validates_acceptance_of :eula, :message =&gt; &quot;must be abided&quot;
  end
</pre>
<p>
If the database column does not exist, the <tt>terms_of_service</tt>
attribute is entirely virtual. This check is performed only if
<tt>terms_of_service</tt> is not <tt>nil</tt> and by default on save.
</p>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - A custom error message (default is: &quot;must be
accepted&quot;).

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:allow_nil</tt> - Skip validation if attribute is <tt>nil</tt> (default
is true).

</li>
<li><tt>:accept</tt> - Specifies value that is considered accepted. The default
value is a string &quot;1&quot;, which makes it easy to relate to an HTML
checkbox. This should be set to <tt>true</tt> if you are validating a
database column, since the attribute is typecast from &quot;1&quot; to
<tt>true</tt> before validation.

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001374-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001374-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 469</span>
469:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_acceptance_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
470:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span>, <span class="ruby-identifier">:allow_nil</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>, <span class="ruby-identifier">:accept</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;1&quot;</span> }
471:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
472: 
473:         <span class="ruby-identifier">db_cols</span> = <span class="ruby-keyword kw">begin</span>
474:           <span class="ruby-identifier">column_names</span>
475:         <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> <span class="ruby-comment cmt"># To ignore both statement and connection errors</span>
476:           []
477:         <span class="ruby-keyword kw">end</span>
478:         <span class="ruby-identifier">names</span> = <span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">reject</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span> <span class="ruby-identifier">db_cols</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>) }
479:         <span class="ruby-identifier">attr_accessor</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">names</span>)
480: 
481:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>,<span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
482:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:accept</span>]
483:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:accepted</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>]) 
484:           <span class="ruby-keyword kw">end</span>
485:         <span class="ruby-keyword kw">end</span>
486:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001382" class="method-detail">
        <a name="M001382"></a>

        <div class="method-heading">
          <a href="#M001382" class="method-signature">
          <span class="method-name">validates_associated</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates whether the associated object or objects are all valid
themselves. Works with any kind of association.
</p>
<pre>
  class Book &lt; ActiveRecord::Base
    has_many :pages
    belongs_to :library

    validates_associated :pages, :library
  end
</pre>
<p>
Warning: If, after the above definition, you then wrote:
</p>
<pre>
  class Page &lt; ActiveRecord::Base
    belongs_to :book

    validates_associated :book
  end
</pre>
<p>
this would specify a circular dependency and cause infinite recursion.
</p>
<p>
NOTE: This validation will not fail if the association hasn&#8216;t been
assigned. If you want to ensure that the association is both present and
guaranteed to be valid, you also need to use <tt><a
href="ClassMethods.html#M001375">validates_presence_of</a></tt>.
</p>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - A custom error message (default is: &quot;is
invalid&quot;)

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001382-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001382-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 901</span>
901:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_associated</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
902:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span> }
903:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
904: 
905:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
906:           <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">:</span> [<span class="ruby-identifier">value</span>]).<span class="ruby-identifier">inject</span>(<span class="ruby-keyword kw">true</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">v</span>, <span class="ruby-identifier">r</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">r</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">r</span>.<span class="ruby-identifier">valid?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">v</span> }
907:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:invalid</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>)
908:           <span class="ruby-keyword kw">end</span>
909:         <span class="ruby-keyword kw">end</span>
910:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001373" class="method-detail">
        <a name="M001373"></a>

        <div class="method-heading">
          <a href="#M001373" class="method-signature">
          <span class="method-name">validates_confirmation_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encapsulates the pattern of wanting to validate a password or email address
field with a confirmation. Example:
</p>
<pre>
  Model:
    class Person &lt; ActiveRecord::Base
      validates_confirmation_of :user_name, :password
      validates_confirmation_of :email_address, :message =&gt; &quot;should match confirmation&quot;
    end

  View:
    &lt;%= password_field &quot;person&quot;, &quot;password&quot; %&gt;
    &lt;%= password_field &quot;person&quot;, &quot;password_confirmation&quot; %&gt;
</pre>
<p>
The added <tt>password_confirmation</tt> attribute is virtual; it exists
only as an in-memory attribute for validating the password. To achieve
this, the validation adds accessors to the model for the confirmation
attribute. NOTE: This check is performed only if
<tt>password_confirmation</tt> is not <tt>nil</tt>, and by default only on
save. To require confirmation, make sure to add a presence check for the
confirmation attribute:
</p>
<pre>
  validates_presence_of :password_confirmation, :if =&gt; :password_changed?
</pre>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - A custom error message (default is: &quot;doesn&#8216;t
match confirmation&quot;).

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001373-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001373-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 433</span>
433:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_confirmation_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
434:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span> }
435:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
436: 
437:         <span class="ruby-identifier">attr_accessor</span>(<span class="ruby-operator">*</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;#{n}_confirmation&quot;</span> }))
438: 
439:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
440:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{attr_name}_confirmation&quot;</span>).<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{attr_name}_confirmation&quot;</span>)
441:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:confirmation</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>]) 
442:           <span class="ruby-keyword kw">end</span>
443:         <span class="ruby-keyword kw">end</span>
444:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001372" class="method-detail">
        <a name="M001372"></a>

        <div class="method-heading">
          <a href="#M001372" class="method-signature">
          <span class="method-name">validates_each</span><span class="method-args">(*attrs) {|record, attr, value| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates each attribute against a block.
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_each :first_name, :last_name do |record, attr, value|
      record.errors.add attr, 'starts with z.' if value[0] == ?z
    end
  end
</pre>
<p>
Options:
</p>
<ul>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:allow_nil</tt> - Skip validation if attribute is <tt>nil</tt>.

</li>
<li><tt>:allow_blank</tt> - Skip validation if attribute is blank.

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001372-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001372-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 391</span>
391:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_each</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attrs</span>)
392:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">extract_options!</span>.<span class="ruby-identifier">symbolize_keys</span>
393:         <span class="ruby-identifier">attrs</span>   = <span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">flatten</span>
394: 
395:         <span class="ruby-comment cmt"># Declare the validation.</span>
396:         <span class="ruby-identifier">send</span>(<span class="ruby-identifier">validation_method</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:on</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">:save</span>), <span class="ruby-identifier">options</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span><span class="ruby-operator">|</span>
397:           <span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span>
398:             <span class="ruby-identifier">value</span> = <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attr</span>)
399:             <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:allow_nil</span>]) <span class="ruby-operator">||</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">blank?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:allow_blank</span>])
400:             <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr</span>, <span class="ruby-identifier">value</span>
401:           <span class="ruby-keyword kw">end</span>
402:         <span class="ruby-keyword kw">end</span>
403:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001381" class="method-detail">
        <a name="M001381"></a>

        <div class="method-heading">
          <a href="#M001381" class="method-signature">
          <span class="method-name">validates_exclusion_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates that the value of the specified attribute is not in a particular
enumerable object.
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_exclusion_of :username, :in =&gt; %w( admin superuser ), :message =&gt; &quot;You don't belong here&quot;
    validates_exclusion_of :age, :in =&gt; 30..60, :message =&gt; &quot;This site is only for under 30 and over 60&quot;
    validates_exclusion_of :format, :in =&gt; %w( mov avi ), :message =&gt; &quot;extension {{value}} is not allowed&quot;
  end
</pre>
<p>
Configuration options:
</p>
<ul>
<li><tt>:in</tt> - An enumerable object of items that the value shouldn&#8216;t
be part of.

</li>
<li><tt>:message</tt> - Specifies a custom error message (default is: &quot;is
reserved&quot;).

</li>
<li><tt>:allow_nil</tt> - If set to true, skips this validation if the
attribute is <tt>nil</tt> (default is <tt>false</tt>).

</li>
<li><tt>:allow_blank</tt> - If set to true, skips this validation if the
attribute is blank (default is <tt>false</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001381-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001381-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 855</span>
855:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_exclusion_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
856:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span> }
857:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
858: 
859:         <span class="ruby-identifier">enum</span> = <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:in</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:within</span>]
860: 
861:         <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;An object with the method include? is required must be supplied as the :in option of the configuration hash&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">enum</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:include?</span>)
862: 
863:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
864:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">enum</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">value</span>)
865:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:exclusion</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>) 
866:           <span class="ruby-keyword kw">end</span>
867:         <span class="ruby-keyword kw">end</span>
868:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001379" class="method-detail">
        <a name="M001379"></a>

        <div class="method-heading">
          <a href="#M001379" class="method-signature">
          <span class="method-name">validates_format_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates whether the value of the specified attribute is of the correct
form by matching it against the regular expression provided.
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_format_of :email, :with =&gt; /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on =&gt; :create
  end
</pre>
<p>
Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the
string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
</p>
<p>
A regular expression must be provided or else an exception will be raised.
</p>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - A custom error message (default is: &quot;is
invalid&quot;).

</li>
<li><tt>:allow_nil</tt> - If set to true, skips this validation if the
attribute is <tt>nil</tt> (default is <tt>false</tt>).

</li>
<li><tt>:allow_blank</tt> - If set to true, skips this validation if the
attribute is blank (default is <tt>false</tt>).

</li>
<li><tt>:with</tt> - The regular expression used to validate the format with
(note: must be supplied!).

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001379-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001379-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 789</span>
789:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_format_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
790:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span>, <span class="ruby-identifier">:with</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">nil</span> }
791:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
792: 
793:         <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;A regular expression must be supplied as the :with option of the configuration hash&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:with</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Regexp</span>)
794: 
795:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
796:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:with</span>]
797:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:invalid</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>) 
798:           <span class="ruby-keyword kw">end</span>
799:         <span class="ruby-keyword kw">end</span>
800:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001380" class="method-detail">
        <a name="M001380"></a>

        <div class="method-heading">
          <a href="#M001380" class="method-signature">
          <span class="method-name">validates_inclusion_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates whether the value of the specified attribute is available in a
particular enumerable object.
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_inclusion_of :gender, :in =&gt; %w( m f ), :message =&gt; &quot;woah! what are you then!??!!&quot;
    validates_inclusion_of :age, :in =&gt; 0..99
    validates_inclusion_of :format, :in =&gt; %w( jpg gif png ), :message =&gt; &quot;extension {{value}} is not included in the list&quot;
  end
</pre>
<p>
Configuration options:
</p>
<ul>
<li><tt>:in</tt> - An enumerable object of available items.

</li>
<li><tt>:message</tt> - Specifies a custom error message (default is: &quot;is
not included in the list&quot;).

</li>
<li><tt>:allow_nil</tt> - If set to true, skips this validation if the
attribute is <tt>nil</tt> (default is <tt>false</tt>).

</li>
<li><tt>:allow_blank</tt> - If set to true, skips this validation if the
attribute is blank (default is <tt>false</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001380-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001380-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 821</span>
821:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_inclusion_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
822:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span> }
823:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
824: 
825:         <span class="ruby-identifier">enum</span> = <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:in</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:within</span>]
826: 
827:         <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;An object with the method include? is required must be supplied as the :in option of the configuration hash&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">enum</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:include?</span>)
828: 
829:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
830:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">enum</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">value</span>)
831:             <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:inclusion</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>) 
832:           <span class="ruby-keyword kw">end</span>
833:         <span class="ruby-keyword kw">end</span>
834:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001376" class="method-detail">
        <a name="M001376"></a>

        <div class="method-heading">
          <a href="#M001376" class="method-signature">
          <span class="method-name">validates_length_of</span><span class="method-args">(*attrs)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates that the specified attribute matches the length restrictions
supplied. Only one option can be used at a time:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_length_of :first_name, :maximum=&gt;30
    validates_length_of :last_name, :maximum=&gt;30, :message=&gt;&quot;less than {{count}} if you don't mind&quot;
    validates_length_of :fax, :in =&gt; 7..32, :allow_nil =&gt; true
    validates_length_of :phone, :in =&gt; 7..32, :allow_blank =&gt; true
    validates_length_of :user_name, :within =&gt; 6..20, :too_long =&gt; &quot;pick a shorter name&quot;, :too_short =&gt; &quot;pick a longer name&quot;
    validates_length_of :fav_bra_size, :minimum =&gt; 1, :too_short =&gt; &quot;please enter at least {{count}} character&quot;
    validates_length_of :smurf_leader, :is =&gt; 4, :message =&gt; &quot;papa is spelled with {{count}} characters... don't play me.&quot;
    validates_length_of :essay, :minimum =&gt; 100, :too_short =&gt; &quot;Your essay must be at least {{count}} words.&quot;), :tokenizer =&gt; lambda {|str| str.scan(/\w+/) }
  end
</pre>
<p>
Configuration options:
</p>
<ul>
<li><tt>:minimum</tt> - The minimum size of the attribute.

</li>
<li><tt>:maximum</tt> - The maximum size of the attribute.

</li>
<li><tt>:is</tt> - The exact size of the attribute.

</li>
<li><tt>:within</tt> - A range specifying the minimum and maximum size of the
attribute.

</li>
<li><tt>:in</tt> - A synonym(or alias) for <tt>:within</tt>.

</li>
<li><tt>:allow_nil</tt> - Attribute may be <tt>nil</tt>; skip validation.

</li>
<li><tt>:allow_blank</tt> - Attribute may be blank; skip validation.

</li>
<li><tt>:too_long</tt> - The error message if the attribute goes over the
maximum (default is: &quot;is too long (maximum is {{count}}
characters)&quot;).

</li>
<li><tt>:too_short</tt> - The error message if the attribute goes under the
minimum (default is: &quot;is too short (min is {{count}}
characters)&quot;).

</li>
<li><tt>:wrong_length</tt> - The error message if using the <tt>:is</tt> method
and the attribute is the wrong size (default is: &quot;is the wrong length
(should be {{count}} characters)&quot;).

</li>
<li><tt>:message</tt> - The error message to use for a <tt>:minimum</tt>,
<tt>:maximum</tt>, or <tt>:is</tt> violation. An alias of the appropriate
<tt>too_long</tt>/<tt>too_short</tt>/<tt>wrong_length</tt> message.

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
<li><tt>:tokenizer</tt> - Specifies how to split up the attribute string. (e.g.
<tt>:tokenizer =&gt; lambda {|str| str.scan(/\w+/)}</tt> to count words as
in above example.) Defaults to <tt>lambda{ |value| value.split(//) }</tt>
which counts individual characters.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001376-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001376-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 556</span>
556:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_length_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attrs</span>)
557:         <span class="ruby-comment cmt"># Merge given options with defaults.</span>
558:         <span class="ruby-identifier">options</span> = {
559:           <span class="ruby-identifier">:tokenizer</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">lambda</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp re">//</span>)}
560:         }.<span class="ruby-identifier">merge</span>(<span class="ruby-constant">DEFAULT_VALIDATION_OPTIONS</span>)
561:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">extract_options!</span>.<span class="ruby-identifier">symbolize_keys</span>)
562: 
563:         <span class="ruby-comment cmt"># Ensure that one and only one range option is specified.</span>
564:         <span class="ruby-identifier">range_options</span> = <span class="ruby-constant">ALL_RANGE_OPTIONS</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">keys</span>
565:         <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">range_options</span>.<span class="ruby-identifier">size</span>
566:           <span class="ruby-keyword kw">when</span> <span class="ruby-value">0</span>
567:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">'Range unspecified.  Specify the :within, :maximum, :minimum, or :is option.'</span>
568:           <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span>
569:             <span class="ruby-comment cmt"># Valid number of options; do nothing.</span>
570:           <span class="ruby-keyword kw">else</span>
571:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">'Too many range options specified.  Choose only one.'</span>
572:         <span class="ruby-keyword kw">end</span>
573: 
574:         <span class="ruby-comment cmt"># Get range option and value.</span>
575:         <span class="ruby-identifier">option</span> = <span class="ruby-identifier">range_options</span>.<span class="ruby-identifier">first</span>
576:         <span class="ruby-identifier">option_value</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">range_options</span>.<span class="ruby-identifier">first</span>]
577: 
578:         <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">option</span>
579:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:within</span>, <span class="ruby-identifier">:in</span>
580:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;:#{option} must be a Range&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Range</span>)
581: 
582:             <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attrs</span>, <span class="ruby-identifier">options</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
583:               <span class="ruby-identifier">value</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:tokenizer</span>].<span class="ruby-identifier">call</span>(<span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">String</span>)
584:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">begin</span>
585:                 <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">:too_short</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:too_short</span>], <span class="ruby-identifier">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">begin</span>)
586:               <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">end</span>
587:                 <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">:too_long</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:too_long</span>], <span class="ruby-identifier">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">end</span>)
588:               <span class="ruby-keyword kw">end</span>
589:             <span class="ruby-keyword kw">end</span>
590:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:is</span>, <span class="ruby-identifier">:minimum</span>, <span class="ruby-identifier">:maximum</span>
591:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;:#{option} must be a nonnegative Integer&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">option_value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">option_value</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">0</span>
592: 
593:             <span class="ruby-comment cmt"># Declare different validations per option.</span>
594:             <span class="ruby-identifier">validity_checks</span> = { <span class="ruby-identifier">:is</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;==&quot;</span>, <span class="ruby-identifier">:minimum</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;&gt;=&quot;</span>, <span class="ruby-identifier">:maximum</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;&lt;=&quot;</span> }
595:             <span class="ruby-identifier">message_options</span> = { <span class="ruby-identifier">:is</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:wrong_length</span>, <span class="ruby-identifier">:minimum</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:too_short</span>, <span class="ruby-identifier">:maximum</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:too_long</span> }
596: 
597:             <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attrs</span>, <span class="ruby-identifier">options</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
598:               <span class="ruby-identifier">value</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:tokenizer</span>].<span class="ruby-identifier">call</span>(<span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">String</span>)
599:               <span class="ruby-keyword kw">unless</span> <span class="ruby-operator">!</span><span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">size</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">validity_checks</span>[<span class="ruby-identifier">option</span>])[<span class="ruby-identifier">option_value</span>]
600:                 <span class="ruby-identifier">key</span> = <span class="ruby-identifier">message_options</span>[<span class="ruby-identifier">option</span>]
601:                 <span class="ruby-identifier">custom_message</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:message</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">key</span>]
602:                 <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">custom_message</span>, <span class="ruby-identifier">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">option_value</span>) 
603:               <span class="ruby-keyword kw">end</span>
604:             <span class="ruby-keyword kw">end</span>
605:         <span class="ruby-keyword kw">end</span>
606:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001383" class="method-detail">
        <a name="M001383"></a>

        <div class="method-heading">
          <a href="#M001383" class="method-signature">
          <span class="method-name">validates_numericality_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates whether the value of the specified attribute is numeric by trying
to convert it to a float with Kernel.Float (if <tt>only_integer</tt> is
false) or applying it to the regular expression <tt>/\A[+\-]?\d+\Z/</tt>
(if <tt>only_integer</tt> is set to true).
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_numericality_of :value, :on =&gt; :create
  end
</pre>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - A custom error message (default is: &quot;is not a
number&quot;).

</li>
<li><tt>:on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>:only_integer</tt> - Specifies whether the value has to be an integer,
e.g. an integral value (default is <tt>false</tt>).

</li>
<li><tt>:allow_nil</tt> - Skip validation if attribute is <tt>nil</tt> (default
is <tt>false</tt>). Notice that for fixnum and float columns empty strings
are converted to <tt>nil</tt>.

</li>
<li><tt>:greater_than</tt> - Specifies the value must be greater than the
supplied value.

</li>
<li><tt>:greater_than_or_equal_to</tt> - Specifies the value must be greater
than or equal the supplied value.

</li>
<li><tt>:equal_to</tt> - Specifies the value must be equal to the supplied
value.

</li>
<li><tt>:less_than</tt> - Specifies the value must be less than the supplied
value.

</li>
<li><tt>:less_than_or_equal_to</tt> - Specifies the value must be less than or
equal the supplied value.

</li>
<li><tt>:odd</tt> - Specifies the value must be an odd number.

</li>
<li><tt>:even</tt> - Specifies the value must be an even number.

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001383-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001383-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 938</span>
938:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_numericality_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
939:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span>, <span class="ruby-identifier">:only_integer</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">false</span>, <span class="ruby-identifier">:allow_nil</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">false</span> }
940:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
941: 
942: 
943:         <span class="ruby-identifier">numericality_options</span> = <span class="ruby-constant">ALL_NUMERICALITY_CHECKS</span>.<span class="ruby-identifier">keys</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">keys</span>
944: 
945:         (<span class="ruby-identifier">numericality_options</span> <span class="ruby-operator">-</span> [ <span class="ruby-identifier">:odd</span>, <span class="ruby-identifier">:even</span> ]).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">option</span><span class="ruby-operator">|</span>
946:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;:#{option} must be a number&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">option</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Numeric</span>)
947:         <span class="ruby-keyword kw">end</span>
948: 
949:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>,<span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
950:           <span class="ruby-identifier">raw_value</span> = <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{attr_name}_before_type_cast&quot;</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">value</span>
951: 
952:           <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:allow_nil</span>] <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">raw_value</span>.<span class="ruby-identifier">nil?</span>
953: 
954:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:only_integer</span>]
955:             <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">raw_value</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/\A[+-]?\d+\Z/</span>
956:               <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:not_a_number</span>, <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">raw_value</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>])
957:               <span class="ruby-keyword kw">next</span>
958:             <span class="ruby-keyword kw">end</span>
959:             <span class="ruby-identifier">raw_value</span> = <span class="ruby-identifier">raw_value</span>.<span class="ruby-identifier">to_i</span>
960:           <span class="ruby-keyword kw">else</span>
961:             <span class="ruby-keyword kw">begin</span>
962:               <span class="ruby-identifier">raw_value</span> = <span class="ruby-constant">Kernel</span>.<span class="ruby-constant">Float</span>(<span class="ruby-identifier">raw_value</span>)
963:             <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-constant">TypeError</span>
964:               <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:not_a_number</span>, <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">raw_value</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>])
965:               <span class="ruby-keyword kw">next</span>
966:             <span class="ruby-keyword kw">end</span>
967:           <span class="ruby-keyword kw">end</span>
968: 
969:           <span class="ruby-identifier">numericality_options</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">option</span><span class="ruby-operator">|</span>
970:             <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">option</span>
971:               <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:odd</span>, <span class="ruby-identifier">:even</span>
972:                 <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">raw_value</span>.<span class="ruby-identifier">to_i</span>.<span class="ruby-identifier">method</span>(<span class="ruby-constant">ALL_NUMERICALITY_CHECKS</span>[<span class="ruby-identifier">option</span>])[]
973:                   <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">option</span>, <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">raw_value</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>]) 
974:                 <span class="ruby-keyword kw">end</span>
975:               <span class="ruby-keyword kw">else</span>
976:                 <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">option</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">raw_value</span>, <span class="ruby-identifier">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">option</span>]) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">raw_value</span>.<span class="ruby-identifier">method</span>(<span class="ruby-constant">ALL_NUMERICALITY_CHECKS</span>[<span class="ruby-identifier">option</span>])[<span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">option</span>]]
977:             <span class="ruby-keyword kw">end</span>
978:           <span class="ruby-keyword kw">end</span>
979:         <span class="ruby-keyword kw">end</span>
980:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001375" class="method-detail">
        <a name="M001375"></a>

        <div class="method-heading">
          <a href="#M001375" class="method-signature">
          <span class="method-name">validates_presence_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates that the specified attributes are not blank (as defined by
Object#blank?). Happens by default on save. Example:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_presence_of :first_name
  end
</pre>
<p>
The first_name attribute must be in the object and it cannot be blank.
</p>
<p>
If you want to validate the presence of a boolean field (where the real
values are true and false), you will want to use <a
href="ClassMethods.html#M001380">validates_inclusion_of</a> :field_name,
:in =&gt; [true, false] This is due to the way Object#blank? handles
boolean values. false.blank? # =&gt; true
</p>
<p>
Configuration options:
</p>
<ul>
<li><tt>message</tt> - A custom error message (default is: &quot;can&#8216;t be
blank&quot;).

</li>
<li><tt>on</tt> - Specifies when this validation is active (default is
<tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).

</li>
<li><tt>if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. :if =&gt; :allow_validation, or :if =&gt;
Proc.new { |user| user.signup_step &gt; 2 }). The method, proc or string
should return or evaluate to a true or false value.

</li>
<li><tt>unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. :unless =&gt; :skip_validation, or
:unless =&gt; Proc.new { |user| user.signup_step &lt;= 2 }). The method,
proc or string should return or evaluate to a true or false value.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001375-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001375-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 510</span>
510:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_presence_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
511:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:save</span> }
512:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
513: 
514:         <span class="ruby-comment cmt"># can't use validates_each here, because it cannot cope with nonexistent attributes,</span>
515:         <span class="ruby-comment cmt"># while errors.add_on_empty can</span>
516:         <span class="ruby-identifier">send</span>(<span class="ruby-identifier">validation_method</span>(<span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:on</span>]), <span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span><span class="ruby-operator">|</span>
517:           <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add_on_blank</span>(<span class="ruby-identifier">attr_names</span>, <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>])
518:         <span class="ruby-keyword kw">end</span>
519:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001377" class="method-detail">
        <a name="M001377"></a>

        <div class="method-heading">
          <span class="method-name">validates_size_of</span><span class="method-args">(*attrs)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="ClassMethods.html#M001376">validates_length_of</a>
</p>
        </div>
      </div>

      <div id="method-M001378" class="method-detail">
        <a name="M001378"></a>

        <div class="method-heading">
          <a href="#M001378" class="method-signature">
          <span class="method-name">validates_uniqueness_of</span><span class="method-args">(*attr_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validates whether the value of the specified attributes are unique across
the system. Useful for making sure that only one user can be named
&quot;davidhh&quot;.
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    validates_uniqueness_of :user_name, :scope =&gt; :account_id
  end
</pre>
<p>
It can also validate whether the value of the specified attributes are
unique based on multiple scope parameters. For example, making sure that a
teacher can only be on the schedule once per semester for a particular
class.
</p>
<pre>
  class TeacherSchedule &lt; ActiveRecord::Base
    validates_uniqueness_of :teacher_id, :scope =&gt; [:semester_id, :class_id]
  end
</pre>
<p>
When the record is created, a check is performed to make sure that no
record exists in the database with the given value for the specified
attribute (that maps to a column). When the record is updated, the same
check is made but disregarding the record itself.
</p>
<p>
Configuration options:
</p>
<ul>
<li><tt>:message</tt> - Specifies a custom error message (default is: &quot;has
already been taken&quot;).

</li>
<li><tt>:scope</tt> - One or more columns by which to limit the scope of the
uniqueness constraint.

</li>
<li><tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text
columns (<tt>true</tt> by default).

</li>
<li><tt>:allow_nil</tt> - If set to true, skips this validation if the
attribute is <tt>nil</tt> (default is <tt>false</tt>).

</li>
<li><tt>:allow_blank</tt> - If set to true, skips this validation if the
attribute is blank (default is <tt>false</tt>).

</li>
<li><tt>:if</tt> - Specifies a method, proc or string to call to determine if
the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or
<tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The
method, proc or string should return or evaluate to a true or false value.

</li>
<li><tt>:unless</tt> - Specifies a method, proc or string to call to determine
if the validation should not occur (e.g. <tt>:unless =&gt;
:skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user|
user.signup_step &lt;= 2 }</tt>). The method, proc or string should return
or evaluate to a true or false value.

</li>
</ul>
<h3>Concurrency and integrity</h3>
<p>
Using this validation method in conjunction with <a
href="../Base.html#M001739">ActiveRecord::Base#save</a> does not guarantee
the absence of duplicate record insertions, because uniqueness checks on
the application level are inherently prone to race conditions. For example,
suppose that two users try to post a Comment at the same time, and a
Comment&#8216;s title must be unique. At the database-level, the actions
performed by these users could be interleaved in the following manner:
</p>
<pre>
              User 1                 |               User 2
 ------------------------------------+--------------------------------------
 # User 1 checks whether there's     |
 # already a comment with the title  |
 # 'My Post'. This is not the case.  |
 SELECT * FROM comments              |
 WHERE title = 'My Post'             |
                                     |
                                     | # User 2 does the same thing and also
                                     | # infers that his title is unique.
                                     | SELECT * FROM comments
                                     | WHERE title = 'My Post'
                                     |
 # User 1 inserts his comment.       |
 INSERT INTO comments                |
 (title, content) VALUES             |
 ('My Post', 'hi!')                  |
                                     |
                                     | # User 2 does the same thing.
                                     | INSERT INTO comments
                                     | (title, content) VALUES
                                     | ('My Post', 'hello!')
                                     |
                                     | # ^^^^^^
                                     | # Boom! We now have a duplicate
                                     | # title!
</pre>
<p>
This could even happen if you use transactions with the
&#8216;serializable&#8217; isolation level. There are several ways to get
around this problem:
</p>
<ul>
<li>By locking the database table before validating, and unlocking it after
saving. However, table locking is very expensive, and thus not recommended.

</li>
<li>By locking a lock file before validating, and unlocking it after saving.
This does not work if you&#8216;ve scaled your <a
href="../../Rails.html">Rails</a> application across multiple web servers
(because they cannot share lock files, or cannot do that efficiently), and
thus not recommended.

</li>
<li>Creating a unique index on the field, by using
ActiveRecord::ConnectionAdapters::SchemaStatements#add_index. In the rare
case that a race condition occurs, the database will guarantee the
field&#8216;s uniqueness.

<p>
When the database catches such a duplicate insertion, <a
href="../Base.html#M001739">ActiveRecord::Base#save</a> will raise an <a
href="../StatementInvalid.html">ActiveRecord::StatementInvalid</a>
exception. You can either choose to let this error propagate (which will
result in the default <a href="../../Rails.html">Rails</a> exception page
being shown), or you can catch it and restart the transaction (e.g. by
telling the user that the title already exists, and asking him to re-enter
the title). This technique is also known as optimistic concurrency control:
<a
href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control">en.wikipedia.org/wiki/Optimistic_concurrency_control</a>
</p>
<p>
Active Record currently provides no way to distinguish unique index
constraint errors from other types of database errors, so you will have to
parse the (database-specific) exception message to detect such a case.
</p>
</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001378-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001378-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 704</span>
704:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validates_uniqueness_of</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attr_names</span>)
705:         <span class="ruby-identifier">configuration</span> = { <span class="ruby-identifier">:case_sensitive</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span> }
706:         <span class="ruby-identifier">configuration</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">attr_names</span>.<span class="ruby-identifier">extract_options!</span>)
707: 
708:         <span class="ruby-identifier">validates_each</span>(<span class="ruby-identifier">attr_names</span>,<span class="ruby-identifier">configuration</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span>, <span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
709:           <span class="ruby-comment cmt"># The check for an existing value should be run from a class that</span>
710:           <span class="ruby-comment cmt"># isn't abstract. This means working down from the current class</span>
711:           <span class="ruby-comment cmt"># (self), to the first non-abstract class. Since classes don't know</span>
712:           <span class="ruby-comment cmt"># their subclasses, we have to build the hierarchy between self and</span>
713:           <span class="ruby-comment cmt"># the record's class.</span>
714:           <span class="ruby-identifier">class_hierarchy</span> = [<span class="ruby-identifier">record</span>.<span class="ruby-identifier">class</span>]
715:           <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">class_hierarchy</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">self</span>
716:             <span class="ruby-identifier">class_hierarchy</span>.<span class="ruby-identifier">insert</span>(<span class="ruby-value">0</span>, <span class="ruby-identifier">class_hierarchy</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">superclass</span>)
717:           <span class="ruby-keyword kw">end</span>
718: 
719:           <span class="ruby-comment cmt"># Now we can work our way down the tree to the first non-abstract</span>
720:           <span class="ruby-comment cmt"># class (which has a database table to query from).</span>
721:           <span class="ruby-identifier">finder_class</span> = <span class="ruby-identifier">class_hierarchy</span>.<span class="ruby-identifier">detect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">klass</span><span class="ruby-operator">|</span> <span class="ruby-operator">!</span><span class="ruby-identifier">klass</span>.<span class="ruby-identifier">abstract_class?</span> }
722: 
723:           <span class="ruby-identifier">is_text_column</span> = <span class="ruby-identifier">finder_class</span>.<span class="ruby-identifier">columns_hash</span>[<span class="ruby-identifier">attr_name</span>.<span class="ruby-identifier">to_s</span>].<span class="ruby-identifier">text?</span>
724: 
725:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span>
726:             <span class="ruby-identifier">comparison_operator</span> = <span class="ruby-value str">&quot;IS ?&quot;</span>
727:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">is_text_column</span>
728:             <span class="ruby-identifier">comparison_operator</span> = <span class="ruby-node">&quot;#{connection.case_sensitive_equality_operator} ?&quot;</span>
729:             <span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_s</span>
730:           <span class="ruby-keyword kw">else</span>
731:             <span class="ruby-identifier">comparison_operator</span> = <span class="ruby-value str">&quot;= ?&quot;</span>
732:           <span class="ruby-keyword kw">end</span>
733: 
734:           <span class="ruby-identifier">sql_attribute</span> = <span class="ruby-node">&quot;#{record.class.quoted_table_name}.#{connection.quote_column_name(attr_name)}&quot;</span>
735: 
736:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> (<span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:case_sensitive</span>] <span class="ruby-operator">||</span> <span class="ruby-operator">!</span><span class="ruby-identifier">is_text_column</span>)
737:             <span class="ruby-identifier">condition_sql</span> = <span class="ruby-node">&quot;#{sql_attribute} #{comparison_operator}&quot;</span>
738:             <span class="ruby-identifier">condition_params</span> = [<span class="ruby-identifier">value</span>]
739:           <span class="ruby-keyword kw">else</span>
740:             <span class="ruby-identifier">condition_sql</span> = <span class="ruby-node">&quot;LOWER(#{sql_attribute}) #{comparison_operator}&quot;</span>
741:             <span class="ruby-identifier">condition_params</span> = [<span class="ruby-identifier">value</span>.<span class="ruby-identifier">mb_chars</span>.<span class="ruby-identifier">downcase</span>]
742:           <span class="ruby-keyword kw">end</span>
743: 
744:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">scope</span> = <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:scope</span>]
745:             <span class="ruby-constant">Array</span>(<span class="ruby-identifier">scope</span>).<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">scope_item</span><span class="ruby-operator">|</span>
746:               <span class="ruby-identifier">scope_value</span> = <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">scope_item</span>)
747:               <span class="ruby-identifier">condition_sql</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot; AND #{record.class.quoted_table_name}.#{scope_item} #{attribute_condition(scope_value)}&quot;</span>
748:               <span class="ruby-identifier">condition_params</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">scope_value</span>
749:             <span class="ruby-keyword kw">end</span>
750:           <span class="ruby-keyword kw">end</span>
751: 
752:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">new_record?</span>
753:             <span class="ruby-identifier">condition_sql</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot; AND #{record.class.quoted_table_name}.#{record.class.primary_key} &lt;&gt; ?&quot;</span>
754:             <span class="ruby-identifier">condition_params</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">:id</span>)
755:           <span class="ruby-keyword kw">end</span>
756: 
757:           <span class="ruby-identifier">finder_class</span>.<span class="ruby-identifier">with_exclusive_scope</span> <span class="ruby-keyword kw">do</span>
758:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">finder_class</span>.<span class="ruby-identifier">exists?</span>([<span class="ruby-identifier">condition_sql</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">condition_params</span>])
759:               <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">:taken</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">configuration</span>[<span class="ruby-identifier">:message</span>], <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>)
760:             <span class="ruby-keyword kw">end</span>
761:           <span class="ruby-keyword kw">end</span>
762:         <span class="ruby-keyword kw">end</span>
763:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>